package org.itboys.core.service.mongo.web;

import org.apache.commons.lang3.StringUtils;
import org.itboys.admin.service.lasted.SundayUploadService;
import org.itboys.commons.ErrorException;
import org.itboys.commons.utils.encryption.Digests;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.commons.utils.tools.LongUtil;
import org.itboys.core.common.SundayCommonConstants;
import org.itboys.core.entity.mongo.SundayStore;
import org.itboys.core.entity.mongo.SundayUser;
import org.itboys.core.util.PhoneUtil;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.itboys.param.PageResult;
import org.itboys.param.ParamUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.*;

/**
 * @Author：贾琦磊
 * @Date：2018/8/3
 * @Description：
 */
@Service
public class SundayUserService extends BaseMongoService<SundayUser>{
    @Autowired
    private SundayUploadService uploadService;
    @Resource(name = "mobileDs")
    private MongoDataSource dataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return dataSource;
    }
    @Override
    protected Class<SundayUser> getEntityClass() {
        return SundayUser.class;
    }

    @Autowired
    private SundayStoreService sundayStoreService;

    public PageResult<SundayUser> select(HttpServletRequest request){
        Map<String, Object> param = ParamUtil.packageMongoExactParam(request,"storeId","type");
        param.put("isDeleted", SundayCommonConstants.NO);
        Map<String, String> vagueParam = ParamUtil.packageMongoVagueParam(request, "realName");
        PageResult<SundayUser> pageResult = super.containpageQuery(request, vagueParam, param);
        List<SundayUser> users = pageResult.getData();
        Set<Long> storeIds=new HashSet<>();
        for (SundayUser user:users
                ) {
            storeIds.add(user.getStoreId());
        }
        param.clear();
        param.put("isDeleted",SundayCommonConstants.NO);
        param.put("id in",storeIds);
        List<SundayStore> stores = sundayStoreService.list(param);
        for (SundayUser user:users
                ) {
            for (SundayStore store:stores
                    ) {
                if(user.getStoreId().equals(store.getId())){
                    user.setStoreName(store.getName());
                }
            }
        }
        pageResult.setData(users);
        return pageResult;
    }

    public void save(Long id,SundayUser user, MultipartFile image) throws IOException {
        Map<String, String> map = uploadService.uploadSingle(image);
        if(StringUtils.isNotEmpty(map.get(SundayUploadService.UPLOAD_HALF_PATH))){
            user.setImage(map.get(SundayUploadService.UPLOAD_HALF_PATH));
        }
        if(!PhoneUtil.isMobileNumber(user.getMobile())){
            throw new ErrorException("手机号格式错误");
        }
        Map<String,Object> param = new HashMap<String,Object>();
        if(!LongUtil.isNotZreo(id)){
            //校验用户名称是否存在
            param.clear();
            param.put("userName",user.getUserName());
            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("账号已存在");
            }
            user.setPassword(Digests.md5(user.getPassword()));
            super.save(user);
        }else{
            param.clear();
            param.put("userName",user.getUserName());
            param.put("id !=",id);

            if(ListUtil.isNotNull(super.list(param))){
                throw new ErrorException("账号已存在");
            }
            if(StringUtils.isNotEmpty(user.getPassword())){
                user.setPassword(Digests.md5(user.getPassword()));
            }
            super.updateExceptEmpty(id,user);
        }
    }

    @Override
    public SundayUser getById(Long id){
        SundayUser user=new SundayUser();
        if(LongUtil.isNotZreo(id)){
            user = super.getById(id);
            SundayStore store = sundayStoreService.getById(user.getStoreId());
            user.setStoreName(store.getName());
        }else {
            user.setId(0);
        }
        return user;
    }
}
